import { Logger } from "../engine/utils/Logger";
import RandomUtil from "../engine/utils/RandomUtil";
import { Config } from "./Config";
import path = require("path")
import fs = require("fs")

export class ServerConfig {

    public static Status_Type = {
        Normal: 1,
        Weihu: 999,
    }

    public serverType: string = ""

    public id: number = 0
    public ip: string;
    public internalIp: string = ""
    public http_port: number = 0;
    public socket_port: number = 0;
    public status: number = 1
    public nowPort: number = 0


    public static account_server_List: Array<ServerConfig> = []
    public static WebServer_List: Array<ServerConfig> = []
    public static gmServer_List: Array<ServerConfig> = []
    public static hall_server_List: Array<ServerConfig> = []


    public static nowServerConfig: ServerConfig

    public static async readConfigFile() {
        let configPath: string = path.resolve() + "/config/"
        if (Config.environment == Config.EnvironmentType.Dev) {
            configPath = configPath + "dev.json"
        }
        else if (Config.environment == Config.EnvironmentType.Test) {
            configPath = configPath + "test.json"
        }
        else if (Config.environment == Config.EnvironmentType.Online) {
            configPath = configPath + "online.json"
        }
        // else if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     configPath = configPath + "yueyouhui.json"
        // }
        // else if (Config.environment == Config.EnvironmentType.xj) {
        //     configPath = configPath + "xj.json"
        // }
        // else if (Config.environment == Config.EnvironmentType.chaoju_new) {
        //     configPath = configPath + "chaojunew.json"
        // }
        // else if (Config.environment == Config.EnvironmentType.Jk) {
        //     configPath = configPath + "jk.json"
        // }
        // else if(Config.environment == Config.EnvironmentType.chaoyou){
        //     configPath = configPath + "chaoyou.json"
        // }
        else {
            Logger.warn("不存在的环境配置文件", configPath)
        }
        // Logger.log("readConfigFile==", configPath)
        let data: Buffer = await fs.readFileSync(configPath);
        // Logger.log("readConfigFile=data=", data.toString())
        let configObj: object = JSON.parse(data.toString())
        return configObj
    }

    private static initOneServerType(configObj: object, nowPort: number) {
        let config: ServerConfig = new ServerConfig()
        config.ip = configObj["ip"];
        config.id = configObj["id"];
        config.internalIp = configObj["internalIp"]
        config.http_port = configObj["http_port"];
        config.socket_port = configObj["socket_port"];
        config.status = configObj["status"]
        if (nowPort == config.http_port || nowPort == config.socket_port) {
            ServerConfig.nowServerConfig = config;
            ServerConfig.nowServerConfig.nowPort = nowPort
        }
        return config;
    }

    public static async initConfig(nowPort: number) {
        let configObj: object = await this.readConfigFile()

        let serverList: Array<object> = configObj[Config.ServerType_List.account_server];
        this.account_server_List = []
        for (let i = 0; i < serverList.length; i++) {
            this.account_server_List[i] = this.initOneServerType(serverList[i], nowPort);
            this.account_server_List[i].serverType = Config.ServerType_List.account_server
        }
        serverList = configObj[Config.ServerType_List.WebServer]
        this.WebServer_List = []
        for (let i = 0; i < serverList.length; i++) {
            this.WebServer_List[i] = this.initOneServerType(serverList[i], nowPort);
            this.WebServer_List[i].serverType = Config.ServerType_List.WebServer
        }

        serverList = configObj[Config.ServerType_List.gm_server]
        this.gmServer_List = []
        for (let i = 0; i < serverList.length; i++) {
            this.gmServer_List[i] = this.initOneServerType(serverList[i], nowPort);
            this.gmServer_List[i].serverType = Config.ServerType_List.gm_server
        }

    }


    public static randomAccountServer() {
        return RandomUtil.randomItem(this.account_server_List)
    }

    public static randomHallServer() {
        return RandomUtil.randomItem(this.hall_server_List)
    }

    public getHttpUrl() {
        return "http://" + this.ip + ":" + this.http_port
    }

    public getInternalUrl() {
        return "http://" + this.internalIp + ":" + this.http_port;
    }

    public getInternalWsUrl() {
        let url: string = "ws://" + this.internalIp + ":" + this.socket_port
        return url
    }


    public static getAllServer() {
        let result: Array<ServerConfig> = []
        result = result.concat(this.WebServer_List)
        result = result.concat(this.account_server_List)
        result = result.concat(this.hall_server_List)
        return result
    }




}